package com.demo.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.demo.model.OpeningReport;
import com.demo.service.OpeningReportService;

@Controller
@RequestMapping("/openingreport")
public class OpeningReportController {

	@Autowired
	private OpeningReportService service;
	
	@RequestMapping("/selectAll.do")
	public String selectAll(Model model){
		List<OpeningReport> listopening = service.selectAll();
		List<OpeningReport> openingFenye = new ArrayList<OpeningReport>();
		int count = listopening.size();
		int n = (count>=5) ? 5:count;
		for(int i=0;i<n;i++ ){
			openingFenye.add(listopening.get(i));
		}
		int zongye = 0;
		if(count%5 == 0){
			zongye = count/5;
		}else{
			zongye = count/5+1;
		}
		int dangqian = 1;
		
		model.addAttribute("listopening", openingFenye);
		model.addAttribute("count", count);
    	model.addAttribute("zongye", zongye);
    	model.addAttribute("dangqian", dangqian);
		return "OpeningReport";
	}
	
	@RequestMapping(value="/selectOpeningFY.do")
	 public String selectUserFY(Integer dangqian,Integer sx,Model model){
		List<OpeningReport> listopening = service.selectAll();
		List<OpeningReport> openingFenye = new ArrayList<OpeningReport>();
	    	
	    	int count = listopening.size();
	    	int zongye = 0;
	    	if(count%5 == 0){
	    		zongye = count/5;
	    	}else{
	    		zongye = count/5+1;
	    	}
	    	if(sx == 1){ 			//上一页
   			dangqian = dangqian - 1;
   		}else if(sx == 2){       //下一页
   			dangqian = dangqian + 1;
   			if(dangqian == zongye);
   			dangqian = -1;
   		}
	    	int n = (count>=5) ? 5:count;
	    	dangqian = (count>=5) ? dangqian:0;

	    	if(dangqian == 0){
	    		for(int i = 0;i < n;i++){
	    			openingFenye.add(listopening.get(i));
		    	}
	    		
		    	dangqian = 1;
	    	}else if(dangqian == -1){
	    		if(count%5 == 0){
		    		zongye = count/5;
		    	}else{
		    		zongye = count/5+1;
		    	}
	    		for(int i = (zongye-1)*5;i < count-1;i++){
	    			openingFenye.add(listopening.get(i));
		    	}
		    	dangqian = zongye;
	    	}else{
	    		for(int i = (dangqian - 1)*5;i < dangqian*5;i++){
	    			openingFenye.add(listopening.get(i));
		    	}
	    	}
	    	
	    	model.addAttribute("listopening", openingFenye);
	    	model.addAttribute("count", count);
	    	model.addAttribute("zongye", zongye);
	    	model.addAttribute("dangqian", dangqian);

			return "OpeningReport";
	  }
	
	@RequestMapping("/selectLike.do")
	public String selectLike(OpeningReport likeopening,Model model){
		System.out.println("listopening--->"+likeopening.getTitle());
		System.out.println("listopening--->"+likeopening.getState());
		List<OpeningReport> listopening = service.selectLike(likeopening.getTitle(),likeopening.getState());
		System.out.println("listopening--->"+listopening);
		model.addAttribute("listopening", listopening);
		return "OpeningReport";
	}
	
	@RequestMapping("/deleteByPrimaryKey.do")
	public String deleteByPrimaryKey(Integer id){
		int i = service.deleteByPrimaryKey(id);
		return "OpeningReport";
	}
	
	@RequestMapping("/deleteBatch.do")
	public void deleteBatch( @RequestParam(value = "ids[]",required = false,defaultValue = "") String[] ids,Model model){
		System.out.println(ids.length);
		service.deleteBatch(ids);
//		List<OpeningReport> listopening = service.selectAll();
//		model.addAttribute("listopening", listopening);
//		return "OpeningReport";
	}
	
	@RequestMapping("/add.do")
	public String add(){
		return "OpeningBasic";
	}
	
	@RequestMapping("/room.do")
	public String room(){
		return "Room";
	}
}
